const app = getApp()

import {
  getMemberInfoRequest,
  findRevenueListRequest
} from '../../api/user'

Page({

  /**
   * 页面的初始数据
   */
  data: {
    pageNo: 1,
    pageSize: 10,
    dataList: [1, 2, 3, 4, 5, 6, 4, 5, 6],
    isLoadingText: '加载中...',
    userInfo: {}
  },

  onLoad: function () {

    /** 初始数据请求 */
    this.loadMore()       // 读列表数据
    this.getUserData()    // 读用户数据

    wx.setNavigationBarTitle({
      title: '我的收益',
    })
  },

  /** 页面相关事件处理函数--监听用户下拉动作 */
  onPullDownRefresh: function () {
    tt.startPullDownRefresh();
    console.log('onPullDownRefresh')
    this.setData({
      pageNo: 1,
      dataList: []
    })
    this.loadMore()
  },

  /** 页面上拉触底事件的处理函数 */
  onReachBottom: function () {
    console.log('onReachBottom')
    let pageIndex = this.data.pageNo;
    this.setData({
      pageNo: pageIndex + 1
    })
    this.loadMore();
  },

  /** 获取列表数据 */
  loadMore: function () {
    let params = {
      pageNo: this.data.pageNo,
      pageSize: this.data.pageSize,
    };
    console.log(params)
    findRevenueListRequest(params).then((res) => {
      console.log('findRevenueListRequest请求成功')
      console.log(res.result.data)
      // this.setData({ dataList: res.result.data });

      let list = (res.result && res.result.data) || [];
      console.log("list")
      console.log(list)
      this.setData({
        isLoadingText: list.length > 0 ? '加载中...' : '没有数据啦~'
      })
      if (this.data.pageNo > 1) {
        //追加数据
        this.setData({
          dataList: this.data.dataList.concat(list)
        })
      } else {
        this.setData({ dataList: list });
        //隐藏导航条加载动画
        tt.hideNavigationBarLoading();
        //停止下拉刷新
        tt.stopPullDownRefresh();
        console.log('dataArr')
        console.log(this.data.dataList)
      }
      // if (res.result.pageNum >= res.result.pages) {
      //   this.finished = true;
      //   this.finishedText = "- 没有更多了-";
      // }
      // if (res.result.pages > this.pageNo) {
      //   this.pageNo++;
      // }
      // this.isLoading = false;

      this.bottomIsLoadingHidden = this.data.dataList.length == this.data.pageSize || this.data.dataList.length == 0;

    }).catch((res) => {
      console.log('getListRequest请求失败')
      console.log(res)
    })
  },

  /** 获取用户信息 */
  getUserData: function () {
    getMemberInfoRequest().then((res) => {
      console.log(res)
      this.setData({
        userInfo: res.result
      })
    }).catch((res) => {
      console.log(res)
    })
  },

  toWithdraw: function (e) {
    // if (this.data.userInfo.surplusRevenue <= 0) {
    //   return
    // }
    tt.navigateTo({
      url: `../withdraw/withdraw?money=${this.data.userInfo.surplusRevenue}` // 指定页面的url
    });
  },


})
